/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cetc.iframe;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.beans.PropertyChangeListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;

import com.cetc.Item;
import com.cetc.Main;
import com.cetc.dao.Dao;
import com.cetc.dao.model.TbGysinfo;
import com.cetc.dao.model.TbRukuDetail;
import com.cetc.dao.model.TbRukuMain;
import com.cetc.dao.model.TbSpinfo;

public class JinHuoDan extends javax.swing.JInternalFrame {

    /**
     * Creates new form JinHuoDan
     */
    public JinHuoDan() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jFrame1 = new javax.swing.JFrame();
        jMenu3 = new javax.swing.JMenu();
        jPopupMenu1 = new javax.swing.JPopupMenu();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        idField = new javax.swing.JTextField();
        jsfsComboBox = new javax.swing.JComboBox<>();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        gysComboBox = new javax.swing.JComboBox<>();
        jhsjField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jsrComboBox = new javax.swing.JComboBox<>();
        lxrField = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        pzslField = new javax.swing.JTextField();
        ysjlField = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        hpzsField = new javax.swing.JTextField();
        czyField = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        hjjeField = new javax.swing.JTextField();
        tjButton = new javax.swing.JButton();
        rukuButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        jMenu1.setText("jMenu1");

        jMenu2.setText("jMenu2");

        jMenuItem1.setText("jMenuItem1");

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        jMenu3.setText("jMenu3");

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("进货单");
        setPreferredSize(new java.awt.Dimension(600, 300));

        jLabel1.setText("进货票号：");
        idField.setEditable(false);// 设置“进货票号”文本框不可编辑

        jLabel2.setText("结算方式：");

        jsfsComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "现金结款", "支票结款" }));

        jLabel3.setText("供应商：");
        
        List gysInfos = Dao.getGysInfos();// 获得供应商信息的集合
		for (Iterator iter = gysInfos.iterator(); iter.hasNext();) {// 遍历供应商信息的集合
			List list = (List) iter.next();// 获得迭代的下一个集合
			Item item = new Item();// 数据表公共类
			item.setId(list.get(0).toString().trim());// 编号属性
			item.setName(list.get(1).toString().trim());// 名称信息
			gysComboBox.addItem(item);// 向“供应商”下拉列表中添加选项
		}
		gysComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				doGysSelectAction();// “供应商”下拉列表的选择事件
			}
		});

        jLabel4.setText("进货时间：");
        
        jLabel5.setText("联系人：");
        lxrField.setEditable(false);
        doGysSelectAction();// “供应商”下拉列表的选择事件

        jLabel6.setText("经手人：");
        
        List<List> czyList = Dao.getJsrs();// 获得被启用的经手人集合
		for (List<String> list : czyList) {// 遍历被启用的经手人集合
			String id = list.get(0);// 经手人编号
			String name = list.get(1);// 经手人姓名
			Item i = new Item(id, name);// 数据表公共类
			i.setId(id + "");// 编号属性
			i.setName(name);// 名称信息
			jsrComboBox.addItem(i);// 向“经手人”下拉列表中添加经手人
		}

        jLabel7.setText("品种数量：");
        pzslField.setEditable(false);// 设置“品种数量”文本框不可编辑

        jLabel8.setText("验收结论：");

        jLabel9.setText("货品总数：");
        hpzsField.setEditable(false);// 设置“货品总数”文本框不可编辑

        jLabel10.setText("操作员：");
        czyField.setEditable(false);// 设置“操作员”文本框不可编辑
		czyField.setText(Main.czyStateLabel.getText());

        czyField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                czyFieldActionPerformed(evt);
            }
        });

        jLabel11.setText("合计金额：");
        hjjeField.setEditable(false);// 设置“合计金额”文本框不可编辑

        tjButton.setText("添加");
        tjButton.addActionListener(new ActionListener() {// 为“添加”按钮添加动作事件的监听
			public void actionPerformed(ActionEvent e) {
				java.sql.Date date = new java.sql.Date(jhsjDate.getTime());// 创建日期对象
				jhsjField.setText(date.toString());// 设置“进货时间”文本框中的文本内容
				String maxId = Dao.getRuKuMainMaxId(date);// 获取最大的“进货票号”
				idField.setText(maxId);// 设置“进货票号”文本框中的文本内容
				// 结束表格中没有编写的单元
				stopTableCellEditing();
				// 如果表格中不包含空行，就添加新行
				for (int i = 0; i <= jTable1.getRowCount() - 1; i++) {
					if (jTable1.getValueAt(i, 0) == null)
						return;
				}
				DefaultTableModel model = (DefaultTableModel) jTable1.getModel();// 创建表格对象
				model.addRow(new Vector());// 向表格添加空行
				getSpComboBox();
			}
		});
        
        rukuButton.setText("入库");
        rukuButton.addActionListener(new java.awt.event.ActionListener() {// 为“入库”按钮添加动作事件的监听
			public void actionPerformed(java.awt.event.ActionEvent e) {
				// 停止表格单元的编辑
				stopTableCellEditing();
				// 清除空行
				clearEmptyRow();
				String pzsStr = pzslField.getText();// 品种数
				String jeStr = hjjeField.getText();// 合计金额
				String jsfsStr = jsfsComboBox.getSelectedItem().toString();// 结算方式
				String jsrStr = jsrComboBox.getSelectedItem() + "";// 经手人
				String czyStr = jsrComboBox.getSelectedItem() + "";// 操作员
				String rkDate = jhsjField.getText();// 入库时间
				String ysjlStr = ysjlField.getText().trim();// 验收结论
				String id = idField.getText();// 票号
				String gysName = gysComboBox.getSelectedItem() + "";// 供应商名字
				if (jsrStr == null || jsrStr.isEmpty()) {// 如果“经手人”下拉列表不存在或“经手人”下拉列表为空
					JOptionPane.showMessageDialog(JinHuoDan.this, "请填写经手人");
					return;
				}
				if (ysjlStr == null || ysjlStr.isEmpty()) {// 如果“验收结论”文本框不存在或“验收结论”文本框为空
					JOptionPane.showMessageDialog(JinHuoDan.this, "填写验收结论");
					return;
				}
				if (jTable1.getRowCount() <= 0) {// 如果表格模型的行数小于等于0
					JOptionPane.showMessageDialog(JinHuoDan.this, "填加入库商品");
					return;
				}
				TbRukuMain ruMain = new TbRukuMain(id, pzsStr, jeStr, ysjlStr, gysName, rkDate, czyStr, jsrStr,
						jsfsStr);// 入库主表
				Set<TbRukuDetail> set = ruMain.getTabRukuDetails();// 入库明细
				int rows = jTable1.getRowCount();// 获得表格模型中的行数
				for (int i = 0; i < rows; i++) {
					TbSpinfo spinfo = (TbSpinfo) jTable1.getValueAt(i, 0);// 商品信息
					if (spinfo == null || spinfo.getId() == null || spinfo.getId().isEmpty())// 商品信息不存在、商品编号不存在或商品编号为空
						continue;// 跳过本次循环，执行下一次循环
					String djStr = (String) jTable1.getValueAt(i, 6);// 单价
					String slStr = (String) jTable1.getValueAt(i, 7);// 数量
					Double dj = Double.valueOf(djStr);// 将String类型的“单价”转换为int型
					Integer sl = Integer.valueOf(slStr);// 将String类型的“数量”转换为int型
					TbRukuDetail detail = new TbRukuDetail();// 入库明细
					detail.setTabSpinfo(spinfo.getId());// 商品信息
					detail.setTabRukuMain(ruMain.getRkId());// 入库主表(入库编号)
					detail.setDj(dj);// 单价
					detail.setSl(sl);// 数量
					set.add(detail);// 添加入库明细
				}
				boolean rs = Dao.insertRukuInfo(ruMain);// 是否成功添加入库信息
				if (rs) {// 成功添加入库信息
					JOptionPane.showMessageDialog(JinHuoDan.this, "入库完成");// 弹出提示框
					DefaultTableModel dftm = new DefaultTableModel();// 创建表格默认模型对象
					jTable1.setModel(dftm);// 将表格的数据模型设置为dftm
					pzslField.setText("0");// 设置“品种数量”文本框中的内容为0
					hpzsField.setText("0");// 设置“货品总数”文本框中的内容为0
					hjjeField.setText("0");// 设置“合计金额”文本框中的内容为0
				}
			}
		});

        String[] bt = null;
        
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                
            },
            bt = new String [] {
                "商品名称", "商品编号", "产地", "单位", "规格", "包装", "单价", "数量", "批号", "批准文号"
            }
        ));
        jTable1.setRowHeight(24);
        jScrollPane1.setViewportView(jTable1);
		// 添加事件完成品种数量、货品总数、合计金额的计算
        jTable1.addContainerListener(new computeInfo());
        
        TableColumn column = jTable1.getColumnModel().getColumn(0);// 以表格模型中索引为0的列作参照
		final DefaultCellEditor editor = new DefaultCellEditor(getSpComboBox());// 创建单元格编辑器)
		editor.setClickCountToStart(2);// 开始编辑单元格所需的单击次数为2次
		column.setCellEditor(editor);// 编辑参照列中单元格时所用的编辑器
		jTable1.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(java.beans.PropertyChangeEvent e) {// 为表格添加更改属性的监听事件
				if ((e.getPropertyName().equals("tableCellEditor"))) {
					new computeInfo();// 事件处理器，该处理器用于计算货品总数、合计金额等信息
				}
			}
		});

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(idField, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jsfsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 15, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jhsjField, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(gysComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(lxrField, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jsrComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(ysjlField, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel10))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(pzslField, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel9)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(czyField, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(hpzsField, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel11)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(tjButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(rukuButton)
                                .addGap(18, 18, 18))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(hjjeField)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(idField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(gysComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(lxrField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4)
                        .addComponent(jhsjField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel6)
                        .addComponent(jsrComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jsfsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(5, 5, 5)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(pzslField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9)
                            .addComponent(hpzsField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel11)
                            .addComponent(hjjeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(ysjlField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10)
                            .addComponent(czyField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tjButton)
                            .addComponent(rukuButton)))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void doGysSelectAction() {// “供应商”下拉列表的选择事件
    	Item item = (Item) gysComboBox.getSelectedItem();// 获得“供应商”下拉列表中被选中的选项
		TbGysinfo gysInfo = Dao.getGysInfo(item);// 供应商信息
		lxrField.setText(gysInfo.getLian());// 设置“联系人”文本框的文本内容
	}
    
    /**
	 * 获得“商品”下拉列表的方法
	 * 
	 * @return “商品”下拉列表
	 */
	private JComboBox getSpComboBox() {
		if (spComboBox == null) {// 如果“商品”下拉列表不存在
			spComboBox = new JComboBox();// 创建“商品”下拉列表
			spComboBox.addItem(new TbSpinfo());// 向“商品”下拉列表中添加商品信息
			spComboBox.addActionListener(new ActionListener() {// 为“商品”下拉列表添加动作事件的监听
				public void actionPerformed(ActionEvent e) {
					ResultSet set = Dao
							.query("select * from tb_spinfo where gysName='" + gysComboBox.getSelectedItem() + "'");// 获得供应商信息的集合
					updateSpComboBox(set);// 更新商品下拉列表
				}
			});
			spComboBox.addItemListener(new java.awt.event.ItemListener() {// 为“商品”下拉列表添加选项事件的监听
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					TbSpinfo info = (TbSpinfo) spComboBox.getSelectedItem();// 获得“商品”下拉列表中被选中的商品信息
					// 如果选择有效就更新表格
					if (info != null && info.getId() != null) {
						updateTable();// 更新表格当前行的内容
					}
				}
			});
		}
		return spComboBox;
	}
    
	/**
	 * 更新商品下拉列表的方法
	 * 
	 * @param set：JDBC返回的ResultSet结果集
	 */
	private void updateSpComboBox(ResultSet set) {
		try {
			while (set.next()) {// 移动后的记录指针指向一条有效的记录
				TbSpinfo spinfo = new TbSpinfo();// 商品信息
				spinfo.setId(set.getString("id").trim());// 商品编号
				spinfo.setSpname(set.getString("spname").trim());// 商品名称
				spinfo.setCd(set.getString("cd").trim());// 产地
				spinfo.setJc(set.getString("jc").trim());// 商品简称
				spinfo.setDw(set.getString("dw").trim());// 商品计量单位
				spinfo.setGg(set.getString("gg").trim());// 商品规格
				spinfo.setBz(set.getString("bz").trim());// 包装
				spinfo.setPh(set.getString("ph").trim());// 批号
				spinfo.setPzwh(set.getString("pzwh").trim());// 批准文号
				spinfo.setMemo(set.getString("memo").trim());// 备注
				spinfo.setGysname(set.getString("gysname").trim());// 供应商名称
				DefaultComboBoxModel model = (DefaultComboBoxModel) spComboBox.getModel();// “商品”下拉列表的默认模型
				if (model.getIndexOf(spinfo) < 0)// “商品”下拉列表不包含该商品
					spComboBox.addItem(spinfo);// 则添加选项
			}
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
	}
	
	/**
	 * 根据商品下拉列表的选择，更新表格当前行的内容
	 */
	private synchronized void updateTable() {
		TbSpinfo spinfo = (TbSpinfo) spComboBox.getSelectedItem();// 获得“商品”下拉列表中被选中的选项
		int row = jTable1.getSelectedRow();// 获得表格模型中被选中的行
		if (row >= 0 && spinfo != null) {// 表格模型中被选中的行大于等于0且“商品”下拉列表中被选中的选项不为空
			// 设置表模型中单元格的值
			jTable1.setValueAt(spinfo.getId(), row, 1);// 商品编号
			jTable1.setValueAt(spinfo.getCd(), row, 2);// 产地
			jTable1.setValueAt(spinfo.getDw(), row, 3);// 商品计量单位
			jTable1.setValueAt(spinfo.getGg(), row, 4);// 商品规格
			jTable1.setValueAt(spinfo.getBz(), row, 5);// 包装
			jTable1.setValueAt("0", row, 6);// 单价
			jTable1.setValueAt("0", row, 7);// 数量
			jTable1.setValueAt(spinfo.getPh(), row, 8);// 批号
			jTable1.setValueAt(spinfo.getPzwh(), row, 9);// 批准文号
			jTable1.editCellAt(row, 6);// 单价可编辑
		}
	}
	
    /**
	 * 该方法用于停止表格单元的编辑
	 */
	private void stopTableCellEditing() {
		TableCellEditor cellEditor = jTable1.getCellEditor();// 创建表格单元编辑器
		if (cellEditor != null)// 表格单元编辑器存在
			cellEditor.stopCellEditing();// 停止表格单元的编辑
	}
	
	// 清除空行
	private synchronized void clearEmptyRow() {
		DefaultTableModel dftm = (DefaultTableModel) jTable1.getModel();// 获得默认的表格模型
		for (int i = 0; i < jTable1.getRowCount(); i++) {
			TbSpinfo spinfo = (TbSpinfo) jTable1.getValueAt(i, 0);// 获得商品信息
			if (spinfo == null || spinfo.getId() == null || spinfo.getId().isEmpty()) {// 商品信息为空
				dftm.removeRow(i);// 移除默认的表格模型中的该行
			}
		}
	}
	
	/**
	 * 事件处理器，该处理器用于计算货品总数、合计金额等信息。
	 */
	public final class computeInfo implements ContainerListener {
		@Override
		public void componentRemoved(ContainerEvent e) {
			// 清除空行
			clearEmptyRow();
			int rows = jTable1.getRowCount();// 获得表格模型中的行数
			int count = 0;// “货品总数”
			double money = 0.0;// “合计金额”
			TbSpinfo column = null;// “商品信息”的实例
			if (rows > 0)// 表格模型中的行数大于0
				column = (TbSpinfo) jTable1.getValueAt(rows - 1, 0);// 为“商品信息”的实例赋值
			if (rows > 0 && (column == null || column.getId().isEmpty()))// 表格模型中的行数大于0且“商品信息”的实例不存在或商品编号为空
				rows--;// 表格模型中的行数减一
			// 计算货品总数和合计金额
			for (int i = 0; i < rows; i++) {
				String column7 = (String) jTable1.getValueAt(i, 7);// 获得表格中“数量”
				String column6 = (String) jTable1.getValueAt(i, 6);// 获得表格中“单价”
				int c7 = (column7 == null || column7.isEmpty()) ? 0 : Integer.parseInt(column7);// 将String类型的“数量”转换为int型
				float c6 = (column6 == null || column6.isEmpty()) ? 0 : Float.parseFloat(column6);// 将String类型的“单价”转换为float型
				count += c7;// 计算货品总数
				money += c6 * c7;// 计算合计金额
			}
			pzslField.setText(rows + "");// 设置“品种数量”文本框中的文本内容
			hpzsField.setText(count + "");// 设置“货品总数”文本框中的文本内容
			hjjeField.setText(money + "");// 设置“合计金额”文本框中的文本内容
		}
		@Override
		public void componentAdded(ContainerEvent e) {
		}
		
	}

    private void czyFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_czyFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_czyFieldActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JinHuoDan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JinHuoDan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JinHuoDan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JinHuoDan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JinHuoDan().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField czyField;
    private javax.swing.JComboBox gysComboBox;
    private javax.swing.JTextField hjjeField;
    private javax.swing.JTextField hpzsField;
    private javax.swing.JTextField idField;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jhsjField;
    private javax.swing.JComboBox jsfsComboBox;
    private javax.swing.JComboBox jsrComboBox;
    private javax.swing.JTextField lxrField;
    private javax.swing.JTextField pzslField;
    private javax.swing.JButton rukuButton;
    private javax.swing.JButton tjButton;
    private javax.swing.JTextField ysjlField;
    private Date jhsjDate = new Date();// 创建日期对象之“进货时间”
	private JComboBox spComboBox = null;// 商品下拉列表
    // End of variables declaration//GEN-END:variables
}
